package com.neusoft.aclome.cmp.cloud.server.jcce.service;

import com.neusoft.aclome.cmp.cloud.server.jcce.constants.ProprietorshipStatusEnum;
import com.neusoft.aclome.cmp.cloud.server.jcce.constants.ResourceApplyStatusEnum;
import com.neusoft.aclome.cmp.cloud.server.jcce.dto.response.DataResourceDTO;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class DataResourceSorter {

    public static void sortDataResourceList(List<DataResourceDTO> dataResourceList) {
        Collections.sort(dataResourceList, new Comparator<DataResourceDTO>() {
            @Override
            public int compare(DataResourceDTO o1, DataResourceDTO o2) {
                // Define the order of ResourceApplyStatusEnum
                List<ProprietorshipStatusEnum> order = Arrays.asList(
                        ProprietorshipStatusEnum.PENDING,
                        ProprietorshipStatusEnum.REJECTED,
                        ProprietorshipStatusEnum.APPROVED
                );

                // First, sort by resourceApplyStatus according to the defined order
                int statusComparison = Integer.compare(
                        order.indexOf(o1.getProprietorshipStatus()),
                        order.indexOf(o2.getProprietorshipStatus())
                );
                if (statusComparison != 0) {
                    return statusComparison;
                }
                // If resourceApplyStatus is the same, sort by createdAt
                return o1.getCreatedAt().compareTo(o2.getCreatedAt());
            }
        });
    }
}